//
//  MCShareMarketingViewController.swift
//  StoreManage
//
//  Created by MC on 2018/10/24.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCShareMarketingViewController: MCViewController {
    
    
    public var activityLogId = 0
    public var dataDict = NSDictionary()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func rightItemClicked() {
        view.window?.addSubview(shareWayView)
        shareWayView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view.window!)
        }
    }
    
    
    // MARK: - Network
    
    func sendNetworking() {
        
        var gooodsImage = ""

        // 去第一个商品的图片
        let goodsList = dataDict["goodsList"] as? NSArray ?? []
        if goodsList.count > 0 {
            let goodsDict = goodsList[0] as? [String:Any] ?? [:]
            gooodsImage = goodsDict["navigateImg1"] as? String ?? ""
        }
        
        
        
        let title = dataDict["title"] as? String ?? ""
        let remark = dataDict["remark"] as? String ?? ""

        
        let startTime = dataDict["startTime"] as? String ?? ""
        let endTime = dataDict["endTime"] as? String ?? ""
        let pickUpStartTime = dataDict["pickUpStartTime"] as? String ?? ""
        let pickUpEndTime = dataDict["pickUpEndTime"] as? String ?? ""

        
        
        
        goodsImageView.sd_setImage(with: URL.init(string: gooodsImage), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
        nameLabel.text = title
        detailLabel.text = remark
        activityTimeLabel.text = "活动时间:" + startTime.MCString_prefix(index: 10) + "~" + endTime.MCString_prefix(index: 10)
        pickUpTimeLabel.text = "取货时间:" + pickUpStartTime.MCString_prefix(index: 10) + "~" + pickUpEndTime.MCString_prefix(index: 10)
        
        let para = [
            "activityLogId" : activityLogId,
            ] as [String : Any]
        
        
        MCNetwork.POST(JAVA_findShareLinkContent, para, vc: self, success: { (data) in
            
            let dataDict = data.dictionaryObject ?? [:]
            let imgPath = dataDict["imgPath"] as? String ?? ""
            
            let manager = SDWebImageManager.init()
            manager.imageDownloader?.downloadImage(with: URL.init(string: imgPath), options: SDWebImageDownloaderOptions(rawValue: 0), progress: { (_, _, _) in
                
            }, completed: { (image, _, _, _) in
                self.qrImageView.image = image
            })
        }, failure: nil)
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "社区团分享"
        self.view.backgroundColor = UIColor.white
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("分享", target: self, selector: #selector(rightItemClicked))
    }
    
    func initUI() {
        
        let bg_W = MCScreenWidth - 50
        let bg_H = bg_W * 1.6
        
        
        view.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(25)
            make.width.equalTo(bg_W)
            make.top.equalTo(50)
            make.height.equalTo(bg_H)
        }
        
        
        bgView.addSubview(bgImageView)
        bgImageView.snp.remakeConstraints { (make) -> Void in
            make.edges.equalTo(bgView)
        }
        let goodsImageWH = bg_W - 25 * 2
        bgImageView.addSubview(titleImageView)
        titleImageView.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(bgImageView)
            make.top.equalTo(30)
            make.size.equalTo(CGSize.init(width: 150, height: 42))
        }
        
        bgImageView.addSubview(goodsImageView)
        goodsImageView.snp.remakeConstraints { (make) -> Void in
            make.centerX.equalTo(bgImageView)
            make.width.height.equalTo(goodsImageWH)
            make.top.equalTo(titleImageView.snp.bottom).offset(20)
        }
        
        bgImageView.addSubview(whiteView)
        whiteView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(bgImageView)
            make.height.equalTo(110)
        }
        
        
        whiteView.addSubview(qrImageView)
        qrImageView.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.centerY.equalTo(whiteView)
            make.width.height.equalTo(80)
        }
        
        
        
        whiteView.addSubview(pickUpTimeLabel)
        pickUpTimeLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(qrImageView.snp.left).offset(-15)
            make.bottom.equalTo(whiteView.snp.bottom).offset(-15)
        }

        whiteView.addSubview(activityTimeLabel)
        activityTimeLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(pickUpTimeLabel)
            make.right.equalTo(qrImageView.snp.left).offset(-15)
            make.bottom.equalTo(pickUpTimeLabel.snp.top).offset(-5)
        }

        whiteView.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(pickUpTimeLabel)
            make.right.equalTo(qrImageView.snp.left).offset(-15)
            make.top.equalTo(whiteView).offset(10)
        }

        whiteView.addSubview(detailLabel)
        detailLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(pickUpTimeLabel)
            make.right.equalTo(qrImageView.snp.left).offset(-15)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
            make.bottom.lessThanOrEqualTo(activityTimeLabel.snp.top).offset(-5)
        }
    }
    
    
    // MARK: - Setter & Getter
    
    lazy var bgView: MCShareBaseBackgroundView = {
        let view = MCShareBaseBackgroundView()
        return view
    }()
    
    lazy var shareWayView: MCShareBaseShareWayView = {
        let view = MCShareBaseShareWayView()
        view.superView = self.view
        view.saveView = self.bgView
        
        return view
    }()
    
    
    private lazy var titleImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Share_marketName")
        return imageView
    }()
    
    
    lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Share_imageBg")
        imageView.layer.cornerRadius = 7
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    
    
    
    lazy var goodsImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        imageView.layer.cornerRadius = 7
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    
    lazy var whiteView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 7
        view.layer.masksToBounds = true
        return view
    }()
    
    private lazy var qrImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        return imageView
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        label.textColor = UIColor.black
        return label
    }()
    
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.numberOfLines = 2
        label.textColor = UIColor.MCGray_middle
        return label
    }()

    lazy var activityTimeLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
    
    lazy var pickUpTimeLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        return label
    }()
}

